﻿#include "friendrequestlist.h"
#include "ui_friendrequestlist.h"
#include "chatsocket.h"
FriendRequestList* FriendRequestList::m_pMian = nullptr;
FriendRequestList::FriendRequestList(QWidget *parent) :
    MyWidget(parent),
    ui(new Ui::FriendRequestList)
{
    ui->setupUi(this);
    setShadow(ui->widget);
    initFriendInfoFromFile();
    ui->label_Icon->setPixmap(QPixmap("res/Icon/joker2.png"));
//    ui->tabWidget->setStyleSheet("border-right:none;");
}

FriendRequestList::~FriendRequestList()
{
    delete ui;
}

void FriendRequestList::addItem(UserInfo &uInfo,QString msg)
{
    MyListWidgetItem2* mwi = new MyListWidgetItem2(this);
    mwi->initAll(uInfo,msg);
    QListWidgetItem * item = new QListWidgetItem(ui->listWidget);
    item->setSizeHint(QSize(744,75));
    item->setBackground(QBrush(QColor(225,225,225)));
    ui->listWidget->addItem(item);
    ui->listWidget->setItemWidget(item,mwi);
    m_vec.append(mwi);
}

void FriendRequestList::addItem(MyListWidgetItem2 *mwi)
{
    QListWidgetItem * item = new QListWidgetItem(ui->listWidget);
    item->setSizeHint(QSize(744,75));
    item->setBackground(QBrush(QColor(225,225,225)));
    ui->listWidget->addItem(item);
    ui->listWidget->setItemWidget(item,mwi);
    m_vec.append(mwi);
}

FriendRequestList *FriendRequestList::getInstance()
{
    if(m_pMian == nullptr)
        m_pMian = new FriendRequestList;
    return m_pMian;
}

void FriendRequestList::updateRequestStatus(const QString &id,int type)
{
    for(auto& obj : m_vec)
    {
        if(obj->getID()==id)
        {
            obj->setLableStuats(type);
            updateFileContent(id,type);
        }
    }
}

void FriendRequestList::updateFileContent(const QString &id,int status)
{
    //修改文件中的状态
    QString filepath;
    QString str = QCoreApplication::applicationDirPath();
    filepath = QString("%1/%2/friendRequest.json").arg(str).arg(id);
    FILE* fp = NULL;
    fp = fopen(filepath.toStdString().c_str(),"r");
    if(!fp)return;
    Json::Value root;
    Json::Reader reader;
    fseek(fp,0,SEEK_END);
    int len = ftell(fp);
    fseek(fp,0,SEEK_SET);
    char* buff = new char[len+1];
    fread(buff,len,1,fp);
    buff[len] = 0;
    if(reader.parse(buff,root))
    {
        for(int i = 0; i < root.size();i++)
        {
            QString sender = root[i]["Sender"].asCString();
            if(sender == id)
            {
                root[i]["Status"] = status;
                break;
            }
        }
    }
    fclose(fp);
    fp = NULL;
    fp = fopen(filepath.toStdString().c_str(),"w");
    if(fp)
    {
        std::string content = root.toStyledString();
        fwrite(content.c_str(),strlen(content.c_str()),1,fp);
        fflush(fp);
        fclose(fp);
    }
    delete [] buff;
    buff = nullptr;
}


void FriendRequestList::on_btn_close_clicked()
{
    close();
}

void FriendRequestList::initFriendInfoFromFile()
{
    QString filepath;
    QString str = QCoreApplication::applicationDirPath();
    QString id = ChatSocket::getInstance()->getUserID();
    filepath = QString("%1/%2/friendRequest.json").arg(str).arg(id);
    FILE* fp = NULL;
    fp = fopen(filepath.toStdString().c_str(),"r");
    if(!fp)return;
    Json::Value root;
    Json::Reader reader;
    fseek(fp,0,SEEK_END);
    int len = ftell(fp);
    fseek(fp,0,SEEK_SET);
    char* buff = new char[len+1];
    fread(buff,len,1,fp);
    buff[len] = 0;
    if(reader.parse(buff,root))
    {
        for(int i = 0; i<root.size();i++)
        {
            UserInfo userinfo;
            QString msg = root[i]["MSG"].asCString();
            userinfo.id = root[i]["Sender"].asCString();
            userinfo.nickname = root[i]["NickName"].asCString();
            userinfo.sex = root[i]["Sex"].asCString();
            userinfo.age = root[i]["Age"].asCString();
            userinfo.headurl = root[i]["HeadUrl"].asCString();
            userinfo.persig = root[i]["PerSig"].asCString();
            userinfo.praise = root[i]["Praise"].asCString();
            userinfo.birthday = root[i]["BirthDay"].asCString();
            userinfo.star = root[i]["Star"].asCString();
            userinfo.address = root[i]["Address"].asCString();
            userinfo.phone = root[i]["Phone"].asCString();
            userinfo.rubric = root[i]["Rubric"].asCString();
            userinfo.zodiac = root[i]["Zodiac"].asCString();
            int status = root[i]["Status" ].asInt();
            MyListWidgetItem2* mwi = new MyListWidgetItem2(this);
            mwi->initAll(userinfo,msg);
            mwi->setLableStuats(status);
            QListWidgetItem * item = new QListWidgetItem(ui->listWidget);
            item->setSizeHint(QSize(744,75));
            item->setBackground(QBrush(QColor(225,225,225)));
            ui->listWidget->addItem(item);
            ui->listWidget->setItemWidget(item,mwi);
            m_vec.append(mwi);
        }
    }
    delete [] buff;
    buff = nullptr;
}

void FriendRequestList::on_btn_min_clicked()
{
    showMinimized();
}
